#include "EditorToolTipWindowViewModel.h"

#include "Font/FontSubsystem.h"

#include "Widgets/Widget.h"

REditorToolTipWindowViewModel::REditorToolTipWindowViewModel()
{
    Font= RFontApplicationSubsystem::GetInstance()->GetDefaultFont();
}

void REditorToolTipWindowViewModel::Tick(float DeltaSeconds)
{
    if(!AroundWidget.IsValid() || !AroundWidget->IsRegistered())
    {
        return;
    }

    ElapsedTime += DeltaSeconds;
    if(ElapsedTime < ShowDelay)
    {
        return;
    }

    //calculate tooltip size
    auto TextSize=RFontApplicationSubsystem::GetInstance()
        ->GetOccupiedSize(Content.Get()
        , Font
        , FontSize
    );
    TooltipSize=TextSize.ToVec2() + SVec2(10,10);

    //calculate tooltip position
    auto AroundWidgetRect=AroundWidget->GetRectInScreenSpace();
    
    //1.try to show below the widget
    ToolTipPosition=SVec2(
        AroundWidgetRect.Min.X + AroundWidgetRect.Extent.X*0.5f - TooltipSize.X*0.5f
        , AroundWidgetRect.Min.Y + AroundWidgetRect.Extent.Y );

}

void REditorToolTipWindowViewModel::Show(const TWidgetAttribute<CString>& InContent, TSharedObjectPtr<RWidget> InAroundWidget)
{
    Content = InContent;
    AroundWidget = InAroundWidget;
    ElapsedTime = 0;
    //ScreenMousePosition=SMouseButtonEvent::GetMosuePositionOnScreen();
}

